/**
 * Doctor class
 */
package docoffice.doss;
import java.sql.ResultSet;
import java.util.logging.Logger;

import docoffice.database.DatabaseManager;


/**
 * @author Ray Nosari
 * @version $Revision: 1.0 $
 */
public class Doctor extends MedicalEmployee{
	
	/**
	 * Field specialty, name
	 */
	private String name, specialty; 
	
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("Doctor");
	
	/**
	 * Constructor for Doctor
	 * @param specialty
	 * @param name
	 */
	public Doctor(String name, String specialty){
		super(name, "Doctor");
		this.name = name;
		this.specialty = specialty;
	}
		
	/**
	 * name getter
	
	 * @return name */
	public String getName(){
		return name;
	}
	
	/**
	 * name setter
	 * @param name
	 */
	public void setName(String name){
		this.name = name;
	}
	
	/**
	 * getter for specialty
	
	 * @return specialty */
	public String getSpecialty(){
		return specialty;
	}
	
	/**
	 * setter for specialty
	 * @param specialty
	 */
	public void setSpecialty(String specialty){
		this.specialty = specialty;
	}

	/**
	 * toString overload method
	
	 * @return ret */
	public String toString(){
		final String ret = "Doctor class contains information about Doctors";
		return ret;
	}
	
	/**
	 * Method createDoctor.
	 * @param username String
	 * @param name String
	 * @param specialty String
	 */
	public void createDoctor(){
		final int id = this.readMedicalEmployeeID();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "INSERT INTO doctor (EmployeeID, Specialty) VALUES" +
				" (" + id + ", '" + this.specialty + "')";
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while creating docotr");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("New doctor successfully created");
	}
	
	/**
	 * Method readDoctor.
	 * @param doc Doctor
	 * @return Doctor
	 */
	public Doctor readDoctor(){
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT * FROM doctor WHERE EmployeeID = " + this.getId();
		ResultSet rs = null;
		Doctor doc = new Doctor(this.name, "");
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			final String specialty = rs.getString("Specialty");
			this.setSpecialty(specialty);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading doctor info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Doctor info read successfully");
		return doc;
	}
	
	/**
	 * Method updateDoctor.
	 * @param doc Doctor
	 */
	public void updateDoctor(){
		final int id = this.readMedicalEmployeeID();
		final String specialty = this.specialty;
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE doctor SET EmployeeID = " + id + 
		        ", Specialty = '" + specialty + "'";

		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while updating doctor info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Doctor info updated successfully");
	}
	
	/**
	 * Method deleteDoctor.
	 * @param doc Doctor
	 */
	public void deleteDoctor(){
		final int id = this.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "DELETE FROM doctor WHERE EmployeeID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while deleting doctor info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		LOG.info("Doctor was deleted successfully");
	}
}